package labb3;

import labb3.FileHandling.LoadSerialized;
import labb3.FileHandling.SaveSerialized;

import java.io.IOException;

/**
 * Used as a "glue" between the UI and the MediaCollection.
 *
 * @author Mikko Puustinen
 * @version 0.4, 2007-okt-17
 */
class MediaController {
    private final MediaCollection mediaCollection;
    private String defaultPath = "collection.mzd";

    /**
     * Construct a MediaController and sets the MediaCollection to use. Will try
     * to load a collection from the default location.
     *
     * @param mediaCollection MediaCollection to use.
     *
     * @see labb3.FileHandling.CollectionLoader
     */
    public MediaController(MediaCollection mediaCollection) {
        this.mediaCollection = mediaCollection;
        try {
            mediaCollection.loadCollection(new LoadSerialized(defaultPath));
        } catch (IOException e) {
            //Don't do anything
        } catch (ClassNotFoundException e) {
            //Don't do anything
        }
    }

    /**
     * Handles both the editing and adding of new Media to the MediaCollection.
     * If the title exist this will be edited, otherwise a new Media will be
     * created with the information specified.
     *
     * @param title    the title to add or edit.
     * @param prodDate production date.
     * @param relDate  release date.
     * @param purDate  purchase date.
     * @param rating   rating of the Media.
     *
     * @throws IllegalArgumentException if the title is empty.
     */
    public void handleMedia(String title, String prodDate, String relDate, String purDate, String rating) throws IllegalArgumentException {
        title = title.trim();
        if (title.isEmpty())
            throw new IllegalArgumentException("Title can not be empty");
        int index = mediaCollection.getTitleIndex(title);
        if (index >= 0) {
            mediaCollection.editMedia(index, prodDate, relDate, purDate, rating);
        } else {
            Media media = new DVD(title);
            media.setProductionDate(prodDate);
            media.setReleaseDate(relDate);
            media.setPurchaseDate(purDate);
            media.setRating(rating);
            mediaCollection.addMedia(media);
        }
    }

    public boolean isTitleInCollection(String title) {
        return mediaCollection.getTitleIndex(title) >= 0;
    }

    /**
     * Remove the specified Media from the MediaCollection.
     *
     * @param media Media object to be removed from the MediaCollection.
     *
     * @throws IndexOutOfBoundsException if the index does not exist in the
     *                                   MediaCollection
     */
    @SuppressWarnings({"UnusedAssignment", "AssignmentToNull"})
    public void removeMedia(Media media) throws IndexOutOfBoundsException {
        int index = mediaCollection.getTitleIndex(media.getTitle());
        if (index < 0)
            throw new IndexOutOfBoundsException("Error while reading collection");
        mediaCollection.removeMedia(index);
        media = null;
    }

    /**
     * Search in the MediaCollection after object with the specified title.
     *
     * @param string title to search for.
     */
    public void search(String string) {
        mediaCollection.notifySearchResult(mediaCollection.searchByTitle(string));
    }

    /**
     * Sort the MediaCollection.
     */
    public void sort() {
        mediaCollection.sortCollection();
    }

    /**
     * Saves the MediaCollection to the specified file.
     *
     * @param path location of the file where the MediaCollection should be
     *             saved.
     *
     * @throws IOException if error occurs when saving.
     */
    public void saveCollection(String path) throws IOException {
        mediaCollection.saveCollection(new SaveSerialized(path));
        defaultPath = path;
    }

    /**
     * Saves the MediaCollection to the default location (collection.mzd)
     *
     * @throws IOException if error occurs when saving.
     */
    public void saveCollection() throws IOException {
        saveCollection(defaultPath);
    }

    /**
     * Loads a MediaCollection from the specified location.
     *
     * @param path location of the file where the MediaCollection will be
     *             loaded.
     *
     * @throws IOException            if error occurs while loading or reading
     *                                the file.
     * @throws ClassNotFoundException Not implemented in this version.
     */
    public void loadCollection(String path) throws IOException, ClassNotFoundException {
        mediaCollection.loadCollection(new LoadSerialized(path));
    }

    /**
     * Returns the state of the MediaCollection.
     *
     * @return true if there has been unsaved changes, false otherwise.
     */
    public boolean isCollectionChanged() {
        return mediaCollection.isChanged();
    }
}
